package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.pojo.cart.Cart;
import com.dongdongshop.pojo.order.TbOrder;
import com.dongdongshop.pojo.order.TbOrderItem;
import com.dongdongshop.pojo.user.TbAddress;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.pojo.vo.OrderItemVo;
import com.dongdongshop.service.order.OrderService;
import com.dongdongshop.service.user.AddressService;
import com.dongdongshop.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Component
public class OrderServiceImpl implements OrderService {

    @Resource
    private TbOrderItemMapper tbOrderItemMapper;

    @Resource
    private TbOrderMapper tbOrderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private AddressService addressService;

    @Autowired
    private IdWorker idWorker;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void submitOrder(Long addressId, Long totalPrice, TbUser user) {
        //根据用的选中的收件人查询iD
        TbAddress address = addressService.getById(addressId);
        //拿到购物车中的数据
        List<Cart> cartList = (List<Cart>) redisTemplate.boundHashOps("cartList").get(user.getUsername());
        //循环购物车 一个商家一个购物车
        for (Cart cart : cartList) {
            //生成order订单
            //订单iD
            long nextId = idWorker.nextId();
            //组装的order //订单表
            TbOrder order = getOder(cart, user, address, nextId);
            //获取去金额
            BigDecimal money = getMoney(cart.getSellerId(), cartList);
            order.setPayment(money);
            //调用增加
            tbOrderMapper.insertSelective(order);
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            addOrder(orderItemList,nextId);
        }
    }

    @Override
    public void updateBuyerRate(long orderId) {
        TbOrder tbOrder = new TbOrder();
        tbOrder.setOrderId(orderId);
        tbOrder.setBuyerRate("1");
        tbOrderMapper.updateByPrimaryKeySelective(tbOrder);
    }

    @Override
    public List<OrderItemVo> getOrderStatus(Long orderId) {
        return tbOrderMapper.getOrderStatus(orderId);
    }


    /*
    * 添加orderItem表
    * */
    private void addOrder(List<TbOrderItem> orderItemList, long nextId) {
        for (TbOrderItem orderItem : orderItemList) {
            orderItem.setId(idWorker.nextId());
            orderItem.setOrderId(nextId);
            tbOrderItemMapper.insertSelective(orderItem);
        }

    }
    /*
    * 组装订单表数据
    * */
    private TbOrder getOder(Cart cart, TbUser user, TbAddress address, long nextId) {
        TbOrder order = new TbOrder();
        order.setOrderId(nextId);
        //1、未付款，2、已付款，3、未发货，4、已发货，5、交易成功，6、交易关闭,7、待评价',
        order.setStatus("1");
        order.setCreateTime(new Date());
        order.setUserId(user.getId().toString());
        order.setBuyerNick(user.getUsername());
        order.setReceiverAreaName(address.getAddress());
        order.setReceiverMobile(address.getMobile());
        order.setReceiver(address.getContact());
        order.setSellerId(cart.getSellerId());
        return  order;
    }

    /*
    * 获取每个商家花的总价格
    * */
    private BigDecimal getMoney(String sellerId, List<Cart> cartList) {
        double totalFee = 0;
        for (Cart cart : cartList) {
            if (cart.getSellerId().equals(sellerId)){
                List<TbOrderItem> orderItemList = cart.getOrderItemList();
                for (TbOrderItem orderItem : orderItemList) {
                    totalFee  += orderItem.getTotalFee().doubleValue();
                }
            }
        }
        return new BigDecimal(totalFee);
    }
}
